﻿using Enuo.Dotnet.Core.Autowired;
using Enuo.Dotnet.Core.Model;
using Enuo.Dotnet.Model;
using Enuo.Dotnet.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Enuo.Dotnet.Web.Controllers
{
  // [Route("api/[controller]")]
  [Route("api/home")]
  [ApiController]
  public class HomeApiController : ControllerBase
  {
    [AutowiredProperty]
    private ArticleService _service { get; set; }

    [AutowiredProperty]
    private MySqlDbContext _dbContext { get; set; }

    //public HomeApiController(ArticleService _service, MySqlDbContext dbContext)
    //{
    //  this._service = _service;
    //  this._dbContext = dbContext;
    //}

    [HttpGet]
    [Route("")]
    public ServcieResult<string> index()
    {
      return new ServcieResult<string>("it works!");
    }

    [HttpGet]
    [Authorize]
    [Route("articles")]
    public ServcieResult<List<Article>> articles()
    {
      return new ServcieResult<List<Article>>(_service.List().Result);
    }

    [HttpGet]
    [Authorize]
    [Route("articles2")]
    public ServcieResult<List<Object>> articles2()
    {
      var result = from article in _dbContext.article
                   from category in _dbContext.category
                   where article.categoryID == category.id
                   select new { id = article.id, category = category.title, title = article.title };

      return new ServcieResult<List<Object>>(result.ToList<Object>());
    }
  }
}
